package com.ice.controller;

import com.ice.pojo.Article;
import com.ice.pojo.Category;
import com.ice.pojo.PageBean;
import com.ice.pojo.Result;
import com.ice.service.ArticleService;
import com.ice.utils.JwtUtil;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    //
    // // 返回新闻列表
    // @GetMapping("/list")
    // public Result<String> list(){//请求头的名称为 Authorization
    //
    //     return Result.success("所有的文章");
    // }

    // 新增文章
    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class)  Article article){
        articleService.add(article);
        return Result.success();

    }

    //文章分类列表查询
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    //获取文章详细信息
    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam  @Validated Integer id){
       Article article = articleService.select(id);
       return Result.success(article);
    }

    // 更新文章
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }

    // 删除文章
    @DeleteMapping
    public  Result delete(@RequestParam Integer id){
        articleService.delete(id);
        return Result.success();
    }



}
